package org.fight.logic;
 
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.factory.message.IMessage;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.date.DateUtil;
import com.sojoys.artifact.factory.bytebuff.IByteBuff;
import com.sojoys.artifact.manager.MessageManager;
import com.sojoys.artifact.manager.ByteBuffManager;
import com.sojoys.artifact.core.IServer;
import com.xiaoleilu.hutool.date.TimeInterval;
import java.util.HashMap;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.tools.ToolError;
import com.xiaoleilu.hutool.log.LogFactory;
import com.sojoys.artifact.tools.ToolMap;
import java.util.Map;
import org.fight.logic.fightbo.FightBO;
 
public class Dispatcher {
	static Log log = LogFactory.get(Dispatcher.class);
	private static final Dispatcher ME = new Dispatcher();
      
    
    public static Dispatcher me() {  
        return ME;  
    }

	/** 逻辑分发入口 */
	public IMessage disp(ISession session,IMessage in){
		IMessage out = MessageManager.me().create();
		TimeInterval timer = DateUtil.timer();
		try {
			short commandId = in.getCommandId();
			out.setCommandId(commandId);
			// 日志分割
			log.debug("----------------------------------------------------------------------------");		
			out.setBody(disp(session,commandId, in));
		} catch (AbstractLogicModelException e) {
			out.setStatus(e.getCode());
		} catch (Exception e) {
			log.error(e);
			out.setStatus(ArtifactErrorCode.UNKNOWN_ERROR);
		} finally {
			// 记录执行时间
			log.debug("commandId : {} execute time : {}",in.getCommandId(), timer.interval());
		}
		
		// 此处不返回，等待异步返回
		if(out.getBody() == null)
			return out; 
			
		session.sendMessage(out);
		return out;
	}

	// 逻辑分发
	public byte[] disp(ISession session,short commandId,IMessage in) throws AbstractLogicModelException {
		byte classCode = (byte) (commandId >> 8);
		switch (classCode) {
			case 100: // 战斗
				return fightBO(session,classCode, (byte) (commandId ^ classCode << 8),in);
			default:
				throw ToolError.newLogicError(ArtifactErrorCode.NOT_FOND_MODULE);
		}
    }
    
    
    /** -----------------------模块开始----------------------- */
    
    
	// 战斗
	private byte[] fightBO(ISession session,byte classCode,byte methodCode,IMessage in) throws AbstractLogicModelException{
		FightBO fightBO = IServer.getModules().getModule(classCode,FightBO.class);
		if(fightBO == null){
			throw ToolError.newLogicError(ArtifactErrorCode.NOT_FOND_MODULE);
		}
		switch (methodCode) {
		case 1: { // 进入战斗 [code = 25601]
	    	log.debug("Remark:{}","进入战斗 [code = 25601]");
		    org.fight.protobuf.C2SFightMsg.ReqEnterFight param = null;
		    try {
		    	param = org.fight.protobuf.C2SFightMsg.ReqEnterFight.parseFrom(in.getBody());
		    } catch (Exception e) {
		    	log.error(e);
		    	throw ToolError.newLogicError(ArtifactErrorCode.PARAM_ERROR);
			}
			log.debug("{}:{}","param",param);		    	
			return fightBO.enterFight(session, param);
		}
		case 2:  // 开始战斗 [code = 25602]
			throw ToolError.newLogicError(ArtifactErrorCode.PARAM_ERROR);
		case 3: { // 操作战斗 [code = 25603]
	    	log.debug("Remark:{}","操作战斗 [code = 25603]");
		    org.fight.protobuf.C2SFightMsg.ReqOperateFight param = null;
		    try {
		    	param = org.fight.protobuf.C2SFightMsg.ReqOperateFight.parseFrom(in.getBody());
		    } catch (Exception e) {
		    	log.error(e);
		    	throw ToolError.newLogicError(ArtifactErrorCode.PARAM_ERROR);
			}
			log.debug("{}:{}","param",param);		    	
			return fightBO.operateFight(session, param);
		}
		case 4:  // 通知网络帧 [code = 25604]
			throw ToolError.newLogicError(ArtifactErrorCode.PARAM_ERROR);
		case 5: { // 结束战斗 [code = 25605]
	    	log.debug("Remark:{}","结束战斗 [code = 25605]");
		    org.fight.protobuf.C2SFightMsg.ReqEndFight param = null;
		    try {
		    	param = org.fight.protobuf.C2SFightMsg.ReqEndFight.parseFrom(in.getBody());
		    } catch (Exception e) {
		    	log.error(e);
		    	throw ToolError.newLogicError(ArtifactErrorCode.PARAM_ERROR);
			}
			log.debug("{}:{}","param",param);		    	
			return fightBO.endFight(session, param);
		}
		case 6: { // 拉取网络帧 [code = 25606]
	    	log.debug("Remark:{}","拉取网络帧 [code = 25606]");
		    org.fight.protobuf.C2SFightMsg.ReqPullNetFrame param = null;
		    try {
		    	param = org.fight.protobuf.C2SFightMsg.ReqPullNetFrame.parseFrom(in.getBody());
		    } catch (Exception e) {
		    	log.error(e);
		    	throw ToolError.newLogicError(ArtifactErrorCode.PARAM_ERROR);
			}
			log.debug("{}:{}","param",param);		    	
			return fightBO.pullNetFrame(session, param);
		}
		default:
			throw ToolError.newLogicError(ArtifactErrorCode.NOT_FOND_MODULE);
		}
	}
}